package com.open.capacity.finance.service.impl;

import com.open.capacity.exception.BusinessException;
import com.open.capacity.finance.config.Globals;
import com.open.capacity.finance.dao.PzReportDao;
import com.open.capacity.finance.dto.*;
import com.open.capacity.finance.service.ReportService;
import com.open.capacity.reptile.entity.GdszczMaEleAcco;
import com.open.capacity.user.service.SysDictDataService;
import com.open.capacity.utils.BigDecimalUtil;
import com.open.capacity.utils.DateUtil;
import com.open.capacity.utils.StringUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;


@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private PzReportDao reportDao;

    @Autowired
    private SysDictDataService dictService;

    @Override
    public List<KmyebDto> kmyeb(Map<String, Object> params) {
        boolean aj = params.containsKey("ajCodes") || params.containsKey("ajCode");
        boolean wldx = params.containsKey("wldxCodes") || params.containsKey("wldxCode");
        boolean pjh = params.containsKey("pjhCodes") || params.containsKey("pjhCode");
        boolean zph = params.containsKey("zphCodes") || params.containsKey("zphCode");
        boolean xm = params.containsKey("xmCodes");
        boolean bm = params.containsKey("bmCodes");
        String pzrqStart = params.get("pzrqStart").toString();
        boolean firstDayOfYear = false;
        if (pzrqStart.endsWith("01-01")) {
            firstDayOfYear = true;
        }
        Integer yefx = -1;
        if (params.containsKey("yefx")) {
            yefx = Integer.parseInt(params.get("yefx").toString());
        }

        List<KmyebDto> dtoList = new ArrayList<>();
        if (aj || wldx || xm || bm || pjh || zph) {
            if (firstDayOfYear) {
                dtoList = reportDao.kmyeb01(params);
            } else {
                dtoList = reportDao.kmyeb2(params);
            }
        } else {
            if (firstDayOfYear) {
                dtoList = reportDao.kmyeb01(params);
            } else {
                dtoList = reportDao.kmyeb1(params);
            }
        }

        boolean isWithoutZero = false;
        if (params.containsKey("isWithoutZero")) {
            isWithoutZero = params.get("isWithoutZero").equals("1");
        }

        KmyebDto jTotal = new KmyebDto();
        jTotal.setKjkmCode("");
        jTotal.setKjkmName("借方合计");
        jTotal.ncye = BigDecimal.ZERO;
        jTotal.qcye = BigDecimal.ZERO;
        jTotal.jfs = BigDecimal.ZERO;
        jTotal.dfs = BigDecimal.ZERO;
        jTotal.jlj = BigDecimal.ZERO;
        jTotal.dlj = BigDecimal.ZERO;
        jTotal.ye = BigDecimal.ZERO;
        jTotal.yefx3 = 1;
        jTotal.yefx1 = 1;
        jTotal.yefx2 = 1;

        KmyebDto dTotal = new KmyebDto();
        dTotal.setKjkmCode("");
        dTotal.setKjkmName("贷方合计");
        dTotal.ncye = BigDecimal.ZERO;
        dTotal.qcye = BigDecimal.ZERO;
        dTotal.jfs = BigDecimal.ZERO;
        dTotal.dfs = BigDecimal.ZERO;
        dTotal.jlj = BigDecimal.ZERO;
        dTotal.dlj = BigDecimal.ZERO;
        dTotal.ye = BigDecimal.ZERO;
        dTotal.yefx3 = 2;
        dTotal.yefx1 = 2;
        dTotal.yefx2 = 2;
        KmyebDto temp;
        for (int i = 0; i < dtoList.size(); i++) {
            temp = dtoList.get(i);
            temp.yefx = 2 - Integer.parseInt(temp.kjkmCode.substring(0, 1)) % 2;
            if (temp.isMx == 1) {
                if (temp.yefx == 2) {
                    temp.ncye = BigDecimalUtil.getData(temp.ncye).negate();
                    temp.qcye = BigDecimalUtil.getData(temp.qcye).negate().add(BigDecimalUtil.getData(temp.ncye));
                    temp.ye = temp.ncye.add(BigDecimalUtil.getData(temp.dlj)).subtract(BigDecimalUtil.getData(temp.jlj));
                } else {
                    temp.ncye = BigDecimalUtil.getData(temp.ncye);
                    temp.qcye = BigDecimalUtil.getData(temp.qcye).add(BigDecimalUtil.getData(temp.ncye));
                    temp.ye = temp.ncye.add(BigDecimalUtil.getData(temp.jlj)).subtract(BigDecimalUtil.getData(temp.dlj));
                }

                if (isWithoutZero && BigDecimalUtil.getData(temp.jlj).compareTo(BigDecimal.ZERO) == 0 && BigDecimalUtil.getData(temp.dlj).compareTo(BigDecimal.ZERO) == 0 && temp.ye.compareTo(BigDecimal.ZERO) == 0) {
                    dtoList.remove(i);
                    i--;
                    continue;
                }

                temp.yefx1 = (temp.ncye == null || BigDecimal.ZERO.compareTo(temp.ncye) == 0 ? 0 : temp.yefx);
                temp.yefx2 = (temp.qcye == null || BigDecimal.ZERO.compareTo(temp.qcye) == 0 ? 0 : temp.yefx);
                temp.yefx3 = (temp.ye == null || BigDecimal.ZERO.compareTo(temp.ye) == 0 ? 0 : temp.yefx);

                jTotal.jfs = jTotal.jfs.add(BigDecimalUtil.getData(temp.jfs));
                jTotal.jlj = jTotal.jlj.add(BigDecimalUtil.getData(temp.jlj));
                dTotal.dfs = dTotal.dfs.add(BigDecimalUtil.getData(temp.dfs));
                dTotal.dlj = dTotal.dlj.add(BigDecimalUtil.getData(temp.dlj));
                if (temp.yefx1 == 2) {
                    dTotal.ncye = dTotal.ncye.add(BigDecimalUtil.getData(temp.ncye));
                } else {
                    jTotal.ncye = jTotal.ncye.add(BigDecimalUtil.getData(temp.ncye));
                }
                if (temp.yefx2 == 2) {
                    dTotal.qcye = dTotal.qcye.add(BigDecimalUtil.getData(temp.qcye));
                } else {
                    jTotal.qcye = jTotal.qcye.add(BigDecimalUtil.getData(temp.qcye));
                }
                if (temp.yefx3 == 2) {
                    dTotal.ye = dTotal.ye.add(BigDecimalUtil.getData(temp.ye));
                } else {
                    jTotal.ye = jTotal.ye.add(BigDecimalUtil.getData(temp.ye));
                }
            }
        }


        for (int i = 0; i < dtoList.size(); i++) {
            KmyebDto parent = dtoList.get(i);
            if (parent.isMx == 0) {
                parent.ncye = BigDecimal.ZERO;
                parent.qcye = BigDecimal.ZERO;
                parent.jfs = BigDecimal.ZERO;
                parent.dfs = BigDecimal.ZERO;
                parent.jlj = BigDecimal.ZERO;
                parent.dlj = BigDecimal.ZERO;
                parent.ye = BigDecimal.ZERO;

                calcParent(dtoList, dtoList.get(i), i);

                if (parent.yefx.equals(2)) {
                    parent.ye = parent.ncye.add(BigDecimalUtil.getData(parent.dlj)).subtract(BigDecimalUtil.getData(parent.jlj));
                } else {
                    parent.ye = parent.ncye.add(BigDecimalUtil.getData(parent.jlj)).subtract(BigDecimalUtil.getData(parent.dlj));
                }
                parent.yefx1 = (parent.ncye == null || BigDecimal.ZERO.compareTo(parent.ncye) == 0 ? 0 : parent.yefx);
                parent.yefx2 = (parent.qcye == null || BigDecimal.ZERO.compareTo(parent.qcye) == 0 ? 0 : parent.yefx);
                parent.yefx3 = (parent.ye == null || BigDecimal.ZERO.compareTo(parent.ye) == 0 ? 0 : parent.yefx);

                if (isWithoutZero && parent.jlj.compareTo(BigDecimal.ZERO) == 0 && parent.dlj.compareTo(BigDecimal.ZERO) == 0 && parent.ye.compareTo(BigDecimal.ZERO) == 0) {
                    dtoList.remove(i);
                    i--;
                }
            }
        }

        //只显示选择的科目
        if (params.containsKey("kjkmCodes_select") && params.containsKey("kjkmCodes")) {
            String[] kjkmCodes = (String[]) params.get("kjkmCodes");
            String[] kjkmCodes_select = (String[]) params.get("kjkmCodes_select");
            if (kjkmCodes.length > kjkmCodes_select.length) {
                List<String> selectKmCodes = Arrays.asList(kjkmCodes_select);
                for (int i = 0; i < dtoList.size(); i++) {
                    if (!selectKmCodes.contains(dtoList.get(i).getKjkmCode())) {
                        dtoList.remove(i);
                        i--;
                    }
                }
            }
        }

        if (!yefx.equals(-1)) {
            Integer finalYefx = yefx;
            dtoList = dtoList.stream().filter(item -> item.yefx3.equals(finalYefx)).collect(Collectors.toList());
        }

        dtoList.add(jTotal);
        dtoList.add(dTotal);

        return dtoList;
    }

    @Override
    public List<FzxyebDto> fzxyeb(Map<String, Object> params) {
        boolean kjkmEnable = params.containsKey("kjkmEnable") && params.get("kjkmEnable").toString().equals("1");
        boolean ajEnable = params.containsKey("ajEnable") && params.get("ajEnable").toString().equals("1");
        boolean pjhEnable = params.containsKey("pjhEnable") && params.get("pjhEnable").toString().equals("1");
        boolean zphEnable = params.containsKey("zphEnable") && params.get("zphEnable").toString().equals("1");
        boolean wldxEnable = params.containsKey("wldxEnable") && params.get("wldxEnable").toString().equals("1");
        boolean xmEnable = params.containsKey("xmEnable") && params.get("xmEnable").toString().equals("1");
        boolean bmEnable = params.containsKey("bmEnable") && params.get("bmEnable").toString().equals("1");
        boolean judgeEnable = params.containsKey("judgeEnable") && params.get("judgeEnable").toString().equals("1");
        boolean jbrqEnable = (params.containsKey("ajJbrqStart") && StringUtil.isNotEmpty(params.get("ajJbrqStart").toString()) || (params.containsKey("ajJbrqEnd") && StringUtil.isNotEmpty(params.get("ajJbrqEnd").toString())));
        boolean pjhYwrqEnable = (params.containsKey("pjhYwrqStart") && StringUtil.isNotEmpty(params.get("pjhYwrqStart").toString()) || (params.containsKey("pjhYwrqEnd") && StringUtil.isNotEmpty(params.get("pjhYwrqEnd").toString())));

        int kjkmXh = StringUtil.toInt(params.get("kjkmXh").toString());
        int ajXh = StringUtil.toInt(params.get("ajXh").toString());
        int pjhXh = StringUtil.toInt(params.get("pjhXh").toString());
        int wldxXh = StringUtil.toInt(params.get("wldxXh").toString());
        int xmXh = StringUtil.toInt(params.get("xmXh").toString());
        int bmXh = StringUtil.toInt(params.get("bmXh").toString());

        boolean isWithoutZero = false;
        if (params.containsKey("isWithoutZero")) {
            isWithoutZero = params.get("isWithoutZero").equals("1");
        }
        Integer yefx = -1;
        if (params.containsKey("yefx")) {
            yefx = Integer.parseInt(params.get("yefx").toString());
        }

        String pzrqStart = params.get("pzrqStart").toString();
        boolean firstDayOfYear = false;
        if (pzrqStart.endsWith("01-01")) {
            firstDayOfYear = true;
        }

        List<FzxyebDto> dtoList = null;
        if (kjkmEnable && !ajEnable && !pjhEnable && !zphEnable && !wldxEnable && !xmEnable && !bmEnable && !judgeEnable && !jbrqEnable && !pjhYwrqEnable) {//单查科目
            params.remove("ajCodes");
            params.remove("wldxCodes");
            params.remove("xmCodes");
            params.remove("bmCodes");
            params.remove("pjhCodes");
            params.remove("zphCodes");
            params.remove("ajJudge");
            params.remove("ajJbrqStart");
            params.remove("ajJbrqEnd");
            params.remove("pjhYwrqStart");
            params.remove("pjhYwrqEnd");
            if (firstDayOfYear) {
                dtoList = reportDao.fzxyeb01(params);
            } else {
                dtoList = reportDao.fzxyeb1(params);
            }
        } else if (kjkmEnable && (ajEnable || pjhEnable || zphEnable || wldxEnable || xmEnable || bmEnable || judgeEnable || jbrqEnable || pjhYwrqEnable) && kjkmXh == 1) {//科目+辅助
            joinFzxyebColumns(params);
            if (firstDayOfYear) {
                dtoList = reportDao.fzxyeb03(params);
            } else {
                dtoList = reportDao.fzxyeb3(params);
            }
        } else if (!kjkmEnable && (ajEnable || pjhEnable || zphEnable || wldxEnable || xmEnable || bmEnable || judgeEnable || jbrqEnable || pjhYwrqEnable)) { //辅助
            joinFzxyebColumns(params);
            if (firstDayOfYear) {
                dtoList = reportDao.fzxyeb02(params);
            } else {
                dtoList = reportDao.fzxyeb2(params);
            }
        } else if ((ajEnable || pjhEnable || zphEnable || wldxEnable || xmEnable || bmEnable || judgeEnable || jbrqEnable || pjhYwrqEnable) && kjkmEnable && kjkmXh > 1) { //辅助+科目
            joinFzxyebColumns(params);
            if (firstDayOfYear) {
                dtoList = reportDao.fzxyeb03(params);
            } else {
                dtoList = reportDao.fzxyeb3(params);
            }
        }


        FzxyebDto jTotal = new FzxyebDto();
        jTotal.setKjkm("");
        jTotal.setKjkm("合计");
        jTotal.ncye = BigDecimal.ZERO;
        jTotal.qcye = BigDecimal.ZERO;
        jTotal.jfs = BigDecimal.ZERO;
        jTotal.dfs = BigDecimal.ZERO;
        jTotal.jlj = BigDecimal.ZERO;
        jTotal.dlj = BigDecimal.ZERO;
        jTotal.ye = BigDecimal.ZERO;
        jTotal.yefx1 = 1;
        jTotal.yefx2 = 1;
        jTotal.yefx3 = 1;


        FzxyebDto temp;
        for (int i = 0; i < dtoList.size(); i++) {
            if (!kjkmEnable || dtoList.get(i).isMx == 1) {
                temp = dtoList.get(i);

                if (temp.yefx == null) {
                    if (kjkmEnable) {
                        temp.yefx = 1;
                    } else {
                        temp.yefx = 2;
                    }
                }

                if ((dtoList.get(i).isMx == null && !kjkmEnable) || dtoList.get(i).isMx == 1) {
                    jTotal.ncye = jTotal.ncye.add(BigDecimalUtil.getData(temp.ncye));
                }

                if (temp.yefx != null && temp.yefx == 2) {
                    temp.ncye = BigDecimalUtil.getData(temp.ncye).negate();
                    temp.qcye = BigDecimalUtil.getData(temp.qcye).negate().add(BigDecimalUtil.getData(temp.ncye));
                    temp.ye = temp.ncye.add(BigDecimalUtil.getData(temp.dlj)).subtract(BigDecimalUtil.getData(temp.jlj));
                } else {
                    temp.ncye = BigDecimalUtil.getData(temp.ncye);
                    temp.qcye = BigDecimalUtil.getData(temp.qcye).add(BigDecimalUtil.getData(temp.ncye));
                    temp.ye = temp.ncye.add(BigDecimalUtil.getData(temp.jlj)).subtract(BigDecimalUtil.getData(temp.dlj));
                }

                if ((dtoList.get(i).isMx == null && !kjkmEnable) || dtoList.get(i).isMx == 1) {
                    if (temp.yefx != null && temp.yefx == 2) {
                        jTotal.qcye = jTotal.qcye.add(BigDecimalUtil.getData(temp.qcye).negate());
                    } else {
                        jTotal.qcye = jTotal.qcye.add(BigDecimalUtil.getData(temp.qcye));
                    }
                }

                temp.yefx1 = (temp.ncye == null || BigDecimal.ZERO.compareTo(temp.ncye) == 0 ? 0 : temp.yefx);
                temp.yefx2 = (temp.qcye == null || BigDecimal.ZERO.compareTo(temp.qcye) == 0 ? 0 : temp.yefx);
                temp.yefx3 = (BigDecimal.ZERO.compareTo(temp.ye) == 0 ? 0 : temp.yefx);

                //if (isWithoutZero && BigDecimalUtil.getData(temp.jlj).compareTo(BigDecimal.ZERO) == 0 && BigDecimalUtil.getData(temp.dlj).compareTo(BigDecimal.ZERO) == 0 && temp.ye.compareTo(BigDecimal.ZERO) == 0) {
                if (isWithoutZero && temp.ye.compareTo(BigDecimal.ZERO) == 0) {
                    dtoList.remove(i);
                    i--;
                    continue;
                }

                if ((dtoList.get(i).isMx == null && !kjkmEnable) || dtoList.get(i).isMx == 1) {
                    jTotal.jfs = jTotal.jfs.add(BigDecimalUtil.getData(temp.jfs));
                    jTotal.jlj = jTotal.jlj.add(BigDecimalUtil.getData(temp.jlj));
                    jTotal.dfs = jTotal.dfs.add(BigDecimalUtil.getData(temp.dfs));
                    jTotal.dlj = jTotal.dlj.add(BigDecimalUtil.getData(temp.dlj));
                }
            }
        }

        if (kjkmEnable) {//只有科目有汇总上级
            for (int i = 0; i < dtoList.size(); i++) {
                FzxyebDto parent = dtoList.get(i);
                if (parent.isMx == 0) {
                    parent.ncye = BigDecimal.ZERO;
                    parent.qcye = BigDecimal.ZERO;
                    parent.jfs = BigDecimal.ZERO;
                    parent.dfs = BigDecimal.ZERO;
                    parent.jlj = BigDecimal.ZERO;
                    parent.dlj = BigDecimal.ZERO;
                    parent.ye = BigDecimal.ZERO;

                    calcParent2(dtoList, dtoList.get(i), i);

                    if (dtoList.get(i).yefx.equals(2)) {
                        dtoList.get(i).ye = parent.ncye.add(BigDecimalUtil.getData(parent.dlj)).subtract(BigDecimalUtil.getData(parent.jlj));
                    } else {
                        dtoList.get(i).ye = parent.ncye.add(BigDecimalUtil.getData(parent.jlj)).subtract(BigDecimalUtil.getData(parent.dlj));
                    }
                    parent.yefx1 = (parent.ncye == null || BigDecimal.ZERO.compareTo(parent.ncye) == 0 ? 0 : parent.yefx);
                    parent.yefx2 = (parent.qcye == null || BigDecimal.ZERO.compareTo(parent.qcye) == 0 ? 0 : parent.yefx);
                    parent.yefx3 = (parent.ye == null || BigDecimal.ZERO.compareTo(parent.ye) == 0 ? 0 : parent.yefx);

                    if (isWithoutZero
                            && parent.jlj.compareTo(BigDecimal.ZERO) == 0
                            && parent.dlj.compareTo(BigDecimal.ZERO) == 0
                            && parent.jfs.compareTo(BigDecimal.ZERO) == 0
                            && parent.dfs.compareTo(BigDecimal.ZERO) == 0
                            && parent.ye.compareTo(BigDecimal.ZERO) == 0) {
                        dtoList.remove(i);
                        i--;
                    }
                }
            }
        }
        jTotal.ye = jTotal.ncye.add(jTotal.jlj).subtract(jTotal.dlj);
        dtoList.add(jTotal);

        for (int i = 0; i < dtoList.size(); i++) {
            if (BigDecimalUtil.getData(dtoList.get(i).ncye).compareTo(BigDecimal.ZERO) < 0) {
                dtoList.get(i).ncye = dtoList.get(i).ncye.abs();
                dtoList.get(i).yefx1 = (3 - dtoList.get(i).yefx1);
            } else if (BigDecimalUtil.getData(dtoList.get(i).ncye).compareTo(BigDecimal.ZERO) == 0) {
                dtoList.get(i).yefx1 = 0;
            }
            if (BigDecimalUtil.getData(dtoList.get(i).qcye).compareTo(BigDecimal.ZERO) < 0) {
                dtoList.get(i).qcye = dtoList.get(i).qcye.abs();
                dtoList.get(i).yefx2 = (3 - dtoList.get(i).yefx2);
            } else if (BigDecimalUtil.getData(dtoList.get(i).qcye).compareTo(BigDecimal.ZERO) == 0) {
                dtoList.get(i).yefx2 = 0;
            }
            if (BigDecimalUtil.getData(dtoList.get(i).ye).compareTo(BigDecimal.ZERO) < 0) {
                dtoList.get(i).ye = dtoList.get(i).ye.abs();
                dtoList.get(i).yefx3 = (3 - dtoList.get(i).yefx3);
            } else if (BigDecimalUtil.getData(dtoList.get(i).ye).compareTo(BigDecimal.ZERO) == 0) {
                dtoList.get(i).yefx3 = 0;
            }
        }

        if (!yefx.equals(-1)) {
            Integer finalYefx = yefx;
            dtoList = dtoList.stream().filter(item -> item.yefx3.equals(finalYefx)).collect(Collectors.toList());
        }

        if (ajXh == 1) { ///案件排第一
            Collections.sort(dtoList, (o1, o2) -> {
                if (StringUtil.isEmpty(o1.ajName) && StringUtil.isNotEmpty(o2.ajName)) {
                    return -1;
                } else if (StringUtil.isNotEmpty(o1.ajName) && StringUtil.isEmpty(o2.ajName)) {
                    return 1;
                } else if (StringUtil.isNotEmpty(o1.ajName) && StringUtil.isNotEmpty(o2.ajName) && o1.ajName.compareTo(o2.ajName) < 0) {
                    return -1;
                } else if (StringUtil.isNotEmpty(o1.ajName) && StringUtil.isNotEmpty(o2.ajName) && o1.ajName.compareTo(o2.ajName) > 0) {
                    return 1;
                } else if (StringUtil.isNotEmpty(o1.kjkmCode) && StringUtil.isNotEmpty(o2.kjkmCode) && o1.kjkmCode.compareTo(o2.kjkmCode) < 0) {
                    return -1;
                } else if (StringUtil.isNotEmpty(o1.kjkmCode) && StringUtil.isNotEmpty(o2.kjkmCode) && o1.kjkmCode.compareTo(o2.kjkmCode) > 0) {
                    return 1;
                } else if (StringUtil.isNotEmpty(o1.pjhName) && StringUtil.isNotEmpty(o2.pjhName) && o1.pjhName.compareTo(o2.pjhName) < 0) {
                    return -1;
                } else if (StringUtil.isNotEmpty(o1.pjhName) && StringUtil.isNotEmpty(o2.pjhName) && o1.pjhName.compareTo(o2.pjhName) > 0) {
                    return 1;
                } else {
                    return 0;
                }
            });
        } else if (pjhXh == 1) { ///票据号排第一
            Collections.sort(dtoList, (o1, o2) -> {
                if (StringUtil.isEmpty(o1.pjhName) && StringUtil.isNotEmpty(o2.pjhName)) {
                    return -1;
                } else if (StringUtil.isNotEmpty(o1.pjhName) && StringUtil.isEmpty(o2.ajName)) {
                    return 1;
                } else if (StringUtil.isNotEmpty(o1.pjhName) && StringUtil.isNotEmpty(o2.pjhName) && o1.pjhName.compareTo(o2.pjhName) < 0) {
                    return -1;
                } else if (StringUtil.isNotEmpty(o1.pjhName) && StringUtil.isNotEmpty(o2.pjhName) && o1.pjhName.compareTo(o2.pjhName) > 0) {
                    return 1;
                } else if (StringUtil.isNotEmpty(o1.kjkmCode) && StringUtil.isNotEmpty(o2.kjkmCode) && o1.kjkmCode.compareTo(o2.kjkmCode) < 0) {
                    return -1;
                } else if (StringUtil.isNotEmpty(o1.kjkmCode) && StringUtil.isNotEmpty(o2.kjkmCode) && o1.kjkmCode.compareTo(o2.kjkmCode) > 0) {
                    return 1;
                } else if (StringUtil.isNotEmpty(o1.ajName) && StringUtil.isNotEmpty(o2.ajName) && o1.ajName.compareTo(o2.ajName) < 0) {
                    return -1;
                } else if (StringUtil.isNotEmpty(o1.ajName) && StringUtil.isNotEmpty(o2.ajName) && o1.ajName.compareTo(o2.ajName) > 0) {
                    return 1;
                } else {
                    return 0;
                }
            });
        } else {
            Collections.sort(dtoList, (o1, o2) -> {
                if (StringUtil.isNotEmpty(o1.kjkmCode) && StringUtil.isNotEmpty(o2.kjkmCode) && o1.kjkmCode.compareTo(o2.kjkmCode) < 0) {
                    return -1;
                } else if (StringUtil.isNotEmpty(o1.kjkmCode) && StringUtil.isNotEmpty(o2.kjkmCode) && o1.kjkmCode.compareTo(o2.kjkmCode) > 0) {
                    return 1;
                } else if (StringUtil.isNotEmpty(o1.ajName) && StringUtil.isNotEmpty(o2.ajName) && o1.ajName.compareTo(o2.ajName) < 0) {
                    return -1;
                } else if (StringUtil.isNotEmpty(o1.ajName) && StringUtil.isNotEmpty(o2.ajName) && o1.ajName.compareTo(o2.ajName) > 0) {
                    return 1;
                } else if (StringUtil.isNotEmpty(o1.pjhName) && StringUtil.isNotEmpty(o2.pjhName) && o1.pjhName.compareTo(o2.pjhName) < 0) {
                    return -1;
                } else if (StringUtil.isNotEmpty(o1.pjhName) && StringUtil.isNotEmpty(o2.pjhName) && o1.pjhName.compareTo(o2.pjhName) > 0) {
                    return 1;
                } else {
                    return 0;
                }
            });
        }

        return dtoList;
    }

    //拼接辅助账列字段
    private void joinFzxyebColumns(Map<String, Object> params) {
        boolean kjkmEnable = params.get("kjkmEnable").toString().equals("1");
        boolean ajEnable = params.get("ajEnable").toString().equals("1");
        boolean pjhEnable = params.get("pjhEnable").toString().equals("1");
        boolean zphEnable = params.get("zphEnable").toString().equals("1");
        boolean wldxEnable = params.get("wldxEnable").toString().equals("1");
        boolean xmEnable = params.get("xmEnable").toString().equals("1");
        boolean bmEnable = params.get("bmEnable").toString().equals("1");
        String select_columns = "";
        String groupby_columns = "";
        String join_nc = "";
        String join_qc = "";
        String join_fs = "";
        String subgroup_columns = "";
        String subselect_columns = "";

        if (kjkmEnable) {
            select_columns += "km.code as kjkm_code,km.name as kjkm_name,is_mx,yefx,";
            subselect_columns += "kjkm_code,";
            groupby_columns += "km.code,km.name,is_mx,yefx,";
            subgroup_columns += "kjkm_code,";
        }
        if (ajEnable) {
            select_columns += "lj.aj_name,";
            subselect_columns += "aj_name,";
            groupby_columns += "lj.aj_name,";
            join_nc += " and lj.aj_name=nc.aj_name";
            join_qc += " and lj.aj_name=qc.aj_name";
            join_fs += " and lj.aj_name=fs.aj_name";
            subgroup_columns += "aj_name,";
        }
        if (pjhEnable) {
            select_columns += "lj.pjh_name,";
            subselect_columns += "pjh_name,";
            groupby_columns += "lj.pjh_name,";
            join_nc += " and lj.pjh_name=nc.pjh_name";
            join_qc += " and lj.pjh_name=qc.pjh_name";
            join_fs += " and lj.pjh_name=fs.pjh_name";
            subgroup_columns += "pjh_name,";
        }
        if (zphEnable) {
            select_columns += "lj.zph_name,";
            subselect_columns += "zph_name,";
            groupby_columns += "lj.zph_name,";
            join_nc += " and lj.zph_name=nc.zph_name";
            join_qc += " and lj.zph_name=qc.zph_name";
            join_fs += " and lj.zph_name=fs.zph_name";
            subgroup_columns += "zph_name,";
        }
        if (wldxEnable) {
            select_columns += "lj.wldx_code,lj.wldx_name,";
            subselect_columns += "wldx_code,wldx_name,";
            groupby_columns += "lj.wldx_code,lj.wldx_name,";
            join_nc += " and lj.wldx_code=nc.wldx_code";
            join_qc += " and lj.wldx_code=qc.wldx_code";
            join_fs += " and lj.wldx_code=fs.wldx_code";
            subgroup_columns += "wldx_code,wldx_name,";
        }
        if (xmEnable) {
            select_columns += "lj.xm_code,lj.xm_name,";
            subselect_columns += "xm_code,xm_name,";
            groupby_columns += "lj.xm_code,lj.xm_name,";
            join_nc += " and lj.xm_code=nc.xm_code";
            join_qc += " and lj.xm_code=qc.xm_code";
            join_fs += " and lj.xm_code=fs.xm_code";
            subgroup_columns += "xm_code,xm_name,";
        }
        if (bmEnable) {
            select_columns += "lj.bm_code,lj.bm_name,";
            subselect_columns += "bm_code,bm_name,";
            groupby_columns += "lj.bm_code,lj.bm_name,";
            join_nc += " and lj.bm_code=nc.bm_code";
            join_qc += " and lj.bm_code=qc.bm_code";
            join_fs += " and lj.bm_code=fs.bm_code";
            subgroup_columns += "bm_code,bm_name,";
        }
        groupby_columns = StringUtil.trimLastChar(groupby_columns);
        select_columns = StringUtil.trimLastChar(select_columns);
        subselect_columns = StringUtil.trimLastChar(subselect_columns);
        subgroup_columns = StringUtil.trimLastChar(subgroup_columns);
        params.put("select_columns", select_columns);
        params.put("subselect_columns", subselect_columns);
        params.put("groupby_columns", groupby_columns);
        params.put("join_nc", join_nc);
        params.put("join_qc", join_qc);
        params.put("join_fs", join_fs);
        params.put("subgroup_columns", subgroup_columns);
    }

    @Override
    public List<SlmxzDto> slmxz(Map<String, Object> params, boolean merge) throws Exception {
        if (!params.containsKey("kjkmCode")) {
            throw new BusinessException("请选择科目！");
        }
        String kjkmCode = params.get("kjkmCode").toString();
        String companyId = params.get("companyId").toString();
        String ztId = params.get("ztId").toString();
        Integer kjnd = Integer.parseInt(params.get("kjnd").toString());
        String pzrqStart = params.get("pzrqStart").toString();
        String pzrqEnd = params.get("pzrqEnd").toString();
        int kjqjStart = DateUtil.parseDate(pzrqStart).getMonth() + 1;
        int kjqjEnd = DateUtil.parseDate(pzrqEnd).getMonth() + 1;

        boolean aj = params.containsKey("ajCodes") || params.containsKey("ajCode");
        boolean wldx = params.containsKey("wldxCodes") || params.containsKey("wldxCode");
        boolean pjh = params.containsKey("pjhCodes") || params.containsKey("pjhCode");
        boolean zph = params.containsKey("zphCodes") || params.containsKey("zphCode");
        boolean xm = params.containsKey("xmCodes");
        boolean bm = params.containsKey("bmCodes");

        List<SlmxzDto> dtoList;
        if (aj || wldx || xm || bm || pjh || zph) {//辅助
            dtoList = reportDao.slmxz2(params);
        } else {
            dtoList = reportDao.slmxz1(params);
        }

        //添加月度和年度汇总记录
        for (int m = kjqjStart; m <= kjqjEnd; m++) {
            if (pzrqStart.compareTo(kjnd + "-01-01") > 0 && m == kjqjStart) {
                summaryMonth(kjnd, m, false, dtoList);
            } else {
                if (pzrqStart.compareTo(kjnd + "-01-01") == 0 && m == kjqjStart) {
                    Optional<SlmxzDto> qcyeOptional = dtoList.stream().filter(item -> item.getTag().equals(1) && item.getZy().equals("期初余额")).findFirst();
                    if (qcyeOptional.isPresent()) {
                        dtoList.remove(qcyeOptional.get());
                    }
                }
                summaryMonth(kjnd, m, true, dtoList);
            }
        }

        Collections.sort(dtoList);
        //计算余额
        calcYe(dtoList, 2 - Integer.parseInt(kjkmCode.substring(0, 1)) % 2); //TODO 根据科目编号计算余额方向

        return dtoList;
    }


    @Override
    public List<SlmxzDto> fzxmxz(Map<String, Object> params, boolean merge) {
        String companyId = params.get("companyId").toString();
        String ztId = params.get("ztId").toString();
        Integer kjnd = Integer.parseInt(params.get("kjnd").toString());
        String pzrqStart = params.get("pzrqStart").toString();
        String pzrqEnd = params.get("pzrqEnd").toString();
        int kjqjStart = DateUtil.parseDate(pzrqStart).getMonth() + 1;
        int kjqjEnd = DateUtil.parseDate(pzrqEnd).getMonth() + 1;

        boolean kjkmEnable = params.containsKey("kjkmEnable") && params.get("kjkmEnable").toString().equals("1");
        boolean ajEnable = params.containsKey("ajEnable") && params.get("ajEnable").toString().equals("1");
        boolean pjhEnable = params.containsKey("pjhEnable") && params.get("pjhEnable").toString().equals("1");
        boolean zphEnable = params.containsKey("zphEnable") && params.get("zphEnable").toString().equals("1");
        boolean wldxEnable = params.containsKey("wldxEnable") && params.get("wldxEnable").toString().equals("1");
        boolean xmEnable = params.containsKey("xmEnable") && params.get("xmEnable").toString().equals("1");
        boolean bmEnable = params.containsKey("bmEnable") && params.get("bmEnable").toString().equals("1");
        boolean judgeEnable = params.containsKey("judgeEnable") && params.get("judgeEnable").toString().equals("1");
        boolean jbrqEnable = (params.containsKey("ajJbrqStart") && StringUtil.isNotEmpty(params.get("ajJbrqStart").toString()) || (params.containsKey("ajJbrqEnd") && StringUtil.isNotEmpty(params.get("ajJbrqEnd").toString())));
        boolean pjhYwrqEnable = (params.containsKey("pjhYwrqStart") && StringUtil.isNotEmpty(params.get("pjhYwrqStart").toString()) || (params.containsKey("pjhYwrqEnd") && StringUtil.isNotEmpty(params.get("pjhYwrqEnd").toString())));


        boolean firstDayOfYear = false;
        if (pzrqStart.endsWith("01-01")) {
            firstDayOfYear = true;
        }
        List<SlmxzDto> dtoList = new ArrayList<>();

        if (kjkmEnable && !ajEnable && !pjhEnable && !zphEnable && !wldxEnable && !xmEnable && !bmEnable && !judgeEnable && !jbrqEnable && !pjhYwrqEnable) {//科目
            params.remove("ajCodes");
            params.remove("wldxCodes");
            params.remove("xmCodes");
            params.remove("bmCodes");
            params.remove("pjhCodes");
            params.remove("zphCodes");
            params.remove("ajJudge");
            params.remove("ajJbrqStart");
            params.remove("ajJbrqEnd");
            params.remove("pjhYwrqStart");
            params.remove("pjhYwrqEnd");
            if (firstDayOfYear) {
                dtoList = reportDao.fzxmxz01(params);
            } else {
                dtoList = reportDao.fzxmxz1(params);
            }
        } else if (ajEnable || pjhEnable || zphEnable || wldxEnable || xmEnable || bmEnable || judgeEnable || jbrqEnable || pjhYwrqEnable) {//辅助
            if (firstDayOfYear) {
                dtoList = reportDao.fzxmxz02(params);
            } else {
                dtoList = reportDao.fzxmxz2(params);
            }
        }

        //添加月度和年度汇总记录
        for (int m = kjqjStart; m <= kjqjEnd; m++) {
            if (pzrqStart.compareTo(kjnd + "-01-01") > 0 && m == kjqjStart) {
                summaryMonth(kjnd, m, false, dtoList);
            } else {
                if (pzrqStart.compareTo(kjnd + "-01-01") == 0 && m == kjqjStart) {
                    Optional<SlmxzDto> qcyeOptional = dtoList.stream().filter(item -> item.getTag().equals(1) && item.getZy().equals("期初余额")).findFirst();
                    if (qcyeOptional.isPresent()) {
                        dtoList.remove(qcyeOptional.get());
                    }
                }
                summaryMonth(kjnd, m, true, dtoList);
            }
        }

        Collections.sort(dtoList);

        Integer yefx = 1;
        if (kjkmEnable) {
            String[] kjkmCodes = (String[]) params.get("kjkmCodes_select");
            if (kjkmCodes != null && kjkmCodes.length == 1) {
                yefx = (2 - Integer.parseInt(kjkmCodes[0].substring(0, 1)) % 2);
            }
        }
        //计算余额
        calcFzxYe(dtoList, yefx);
        return dtoList;
    }

    @Override
    public ZbBookDto slmxzBook(Map<String, Object> params, boolean merge) throws Exception {
        if (!params.containsKey("kjkmCodes")) {
            throw new BusinessException("请选择科目！");
        }
        String kjkmCodesStr = params.get("kjkmCodes_select").toString();
        String[] kjkmArr = kjkmCodesStr.split(",");
        // params.put("kjkmCodes", kjkmArr);
        String companyId = params.get("companyId").toString();
        String ztId = params.get("ztId").toString();
        Integer kjnd = Integer.parseInt(params.get("kjnd").toString());
        String pzrqStart = params.get("pzrqStart").toString();
        String pzrqEnd = params.get("pzrqEnd").toString();
        int kjqjStart = DateUtil.parseDate(pzrqStart).getMonth() + 1;
        int kjqjEnd = DateUtil.parseDate(pzrqEnd).getMonth() + 1;
        boolean ajEnable = params.get("ajEnable").toString().equals("1");
        boolean pjhEnable = params.get("pjhEnable").toString().equals("1");
        boolean zphEnable = params.get("zphEnable").toString().equals("1");
        boolean wldxEnable = params.get("wldxEnable").toString().equals("1");
        boolean xmEnable = params.get("xmEnable").toString().equals("1");
        boolean bmEnable = params.get("bmEnable").toString().equals("1");
        boolean firstDayOfYear = false;
        if (pzrqStart.endsWith("01-01")) {
            firstDayOfYear = true;
        }

        List<SlmxzzbDto> dtoList;
        TreeMap<String, List<SlmxzzbDto>> listMap;
        if (ajEnable || pjhEnable || zphEnable || wldxEnable || xmEnable || bmEnable) { //科目+辅助
            params.remove("ajEnable");
            params.remove("pjhEnable");
            params.remove("zphEnable");
            params.remove("wldxEnable");
            params.remove("xmEnable");
            params.remove("bmEnable");

            if (firstDayOfYear) {
                dtoList = reportDao.kmslzzb02(params);
            } else {
                dtoList = reportDao.kmslzzb2(params);
            }
        } else {//科目
            if (firstDayOfYear) {
                dtoList = reportDao.kmslzzb01(params);
            } else {
                dtoList = reportDao.kmslzzb1(params);
            }
        }

        //只显示选择的科目
        if (params.containsKey("kjkmCodes_select") && params.containsKey("kjkmCodes")) {
            String[] kjkmCodes = (String[]) params.get("kjkmCodes");
            String[] kjkmCodes_select = (String[]) params.get("kjkmCodes_select");
            if (kjkmCodes.length > kjkmCodes_select.length) {
                List<String> selectKmCodes = Arrays.asList(kjkmCodes_select);
                for (int i = 0; i < dtoList.size(); i++) {
                    if (!selectKmCodes.contains(dtoList.get(i).getKmCode())) {
                        dtoList.remove(i);
                        i--;
                    }
                }
            }
        }

        listMap = dtoList.stream().filter(item -> StringUtil.isNotEmpty(item.getKmCode())).collect(Collectors.groupingBy(item -> item.getKmName(), TreeMap::new, Collectors.toList()));

        //组装辅助核算
        listMap = groupByFzhs(listMap, ajEnable, pjhEnable, zphEnable, wldxEnable, xmEnable, bmEnable);

        boolean isWithoutZero = false;
        if (params.containsKey("isWithoutZero")) {
            isWithoutZero = params.get("isWithoutZero").equals("1");
        }


        Iterator<Map.Entry<String, List<SlmxzzbDto>>> it = listMap.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, List<SlmxzzbDto>> entry = it.next();
            List<SlmxzzbDto> entryValue = entry.getValue();
            if (pzrqStart.equals(kjnd + "-01-01")) {//1月1日显示年初余额
                //初始化年初余额记录
                initNcye1(kjnd, entryValue);
            }
            List<SlmxzzbDto> ncyeList = entryValue.stream().filter(t -> t.getTag().equals(0)).collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(ncyeList) && ncyeList.size() > 1) {
                SlmxzzbDto firstNcye = ncyeList.get(0);
                SlmxzzbDto newNcye = new SlmxzzbDto();
                newNcye.setTag(firstNcye.getTag());
                newNcye.setKjqj(firstNcye.getKjqj());
                newNcye.setKmCode(firstNcye.getKmCode());
                newNcye.setKmName(firstNcye.getKmName());
                newNcye.setZy("年初余额");
                newNcye.setPzid(firstNcye.getPzid());
                newNcye.setPzrq(firstNcye.getPzrq());
                newNcye.setPzh(firstNcye.getPzh());
                newNcye.setYefx(firstNcye.getYefx());
                newNcye.setYefxStr(firstNcye.getYefxStr());
                newNcye.setJ(BigDecimal.ZERO);
                newNcye.setD(BigDecimal.ZERO);
                BigDecimal ye = BigDecimal.ZERO;
                for (int i = 0; i < ncyeList.size(); i++) {
                    ye = ye.add(BigDecimalUtil.getData(ncyeList.get(i).getYe()));
                }
                newNcye.setYe(ye);

                for (int i = 0; i < entryValue.size(); i++) {
                    if (entryValue.get(i).getTag().equals(0)) {
                        entryValue.remove(i);
                        i--;
                    }
                }

                entryValue.add(0, newNcye);
            }
            if (isWithoutZero && entryValue.size() == 1) {
                if (entryValue.get(0).getYe() != null && entryValue.get(0).getYe().compareTo(BigDecimal.ZERO) == 0) {
                    entryValue.remove(0);
                    it.remove();
                }
            }
        }


        for (Map.Entry<String, List<SlmxzzbDto>> entry : listMap.entrySet()) {
            String kmCode = entry.getKey().split("-")[0].split(" ")[0];
            List<SlmxzzbDto> entryValue = entry.getValue();

            //添加月度和年度汇总记录
            for (int m = kjqjStart; m <= kjqjEnd; m++) {
                if (pzrqStart.compareTo(kjnd + "-01-01") > 0 && m == kjqjStart) {
                    summaryMonth1(kjnd, m, false, entryValue);
                } else {
                    summaryMonth1(kjnd, m, true, entryValue);
                }
            }
            Collections.sort(entry.getValue());
            //计算余额
            calcYe(entry.getValue(), (2 - Integer.parseInt(kmCode.substring(0, 1)) % 2));
        }

        ZbBookDto dto = new ZbBookDto();
        List<ZbIndexDto> indexList = new ArrayList<>(listMap.size());
        List<ZbTitleDto> contentList = new ArrayList<>();
        ZbIndexDto index;
        ZbTitleDto content;

        String isNj = "";

        int currentPage = 1; //pageSlzzb(listMap.size());
        for (Map.Entry<String, List<SlmxzzbDto>> entry : listMap.entrySet()) {
            index = new ZbIndexDto();
            index.setCode(entry.getKey());
            index.setName(entry.getKey());
            int page = entry.getValue().size() <= 33 ? 1 : (entry.getValue().size() / 33 + (entry.getValue().size() % 33 == 0 ? 0 : 1));
            if (page == 1) {
                index.setPage(String.format("%s", currentPage));
            } else if (page > 1) {
                index.setPage(String.format("%s-%s", currentPage, currentPage + page - 1));
            }
            currentPage += page;
            indexList.add(index);

            content = new ZbTitleDto();
            content.setZtName(dictService.findNameByValue("ZT", ztId));
            content.setKjkmName(entry.getKey());
            content.setPzrq(pzrqStart.substring(0, 10) + "至" + pzrqEnd.substring(0, 10) + isNj);
            content.setPzflList(entry.getValue());
            contentList.add(content);
        }
        dto.setIndex(indexList);
        dto.setContent(contentList);
        return dto;
    }

    //计算三栏账账本页码
    private int pageSlzzb(int size) {
        int page = 2;
        if (size <= 35) {
            page = 2;
        } else {
            page = 2 + (size - 35) / 38 + 1;
        }
        return page;
    }

    @Override
    public ZbBookDto kmzzBook(Map<String, Object> params, boolean merge) {
        if (!params.containsKey("kjkmCodes")) {
            throw new BusinessException("请选择科目！");
        }
//        String kjkmCodes = params.get("kjkmCodes_select").toString();
//        String[] kjkmArr = kjkmCodes.split(",");

        String companyId = params.get("companyId").toString();
        String ztId = params.get("ztId").toString();
        Integer kjnd = Integer.parseInt(params.get("kjnd").toString());

        String pzrqStart = params.get("pzrqStart").toString();
        String pzrqEnd = params.get("pzrqEnd").toString();
        int kjqjStart = DateUtil.parseDate(pzrqStart).getMonth() + 1;
        int kjqjEnd = DateUtil.parseDate(pzrqEnd).getMonth() + 1;
        List<SlmxzDto> dtoList;
        if (merge) {
            dtoList = reportDao.kmzzzb(params);
        } else {
            dtoList = reportDao.kmzzzb(params);
        }

        //只显示选择的科目
        if (params.containsKey("kjkmCodes_select") && params.containsKey("kjkmCodes")) {
            String[] kjkmCodes = (String[]) params.get("kjkmCodes");
            String[] kjkmCodes_select = (String[]) params.get("kjkmCodes_select");
            if (kjkmCodes.length >= kjkmCodes_select.length) {
                List<String> selectKmCodes = Arrays.asList(kjkmCodes_select);
                for (int i = 0; i < dtoList.size(); i++) {
                    if (!selectKmCodes.contains(dtoList.get(i).getKmCode())) {
                        dtoList.remove(i);
                        i--;
                    }
                }
            }
        }
        //TreeMap<String, List<SlmxzDto>> listMap = dtoList.stream().filter(item -> StringUtil.isNotEmpty(item.getKmCode())).collect(Collectors.groupingBy(item -> item.kmCode.substring(0, codeRule.getLevel1()), TreeMap::new, Collectors.toList()));
        TreeMap<String, List<SlmxzDto>> listMap = dtoList.stream().filter(item -> StringUtil.isNotEmpty(item.getKmCode())).collect(Collectors.groupingBy(item -> item.kmCode, TreeMap::new, Collectors.toList()));


        for (Map.Entry<String, List<SlmxzDto>> entry : listMap.entrySet()) {
            List<SlmxzDto> result = new ArrayList<>(entry.getValue().size());
            String kjkmCode = entry.getKey();
            Integer yefx = (2 - Integer.parseInt(entry.getKey().substring(0, 1)) % 2);
            SlmxzDto md;
            BigDecimal preYe = BigDecimal.ZERO;
            boolean exists = false;
            SlmxzDto ncye = null;
            for (int i = 0; i < entry.getValue().size(); i++) {
                if (entry.getValue().get(i).tag.equals(0)) {
                    if (!exists) {
                        md = new SlmxzDto();
                        md.tag = entry.getValue().get(i).tag;
                        md.kmCode = entry.getValue().get(i).kmCode;
                        md.kmName = entry.getValue().get(i).kmName;
                        md.kjqj = entry.getValue().get(i).kjqj;
                        md.j = entry.getValue().get(i).j;
                        md.d = entry.getValue().get(i).d;
                        md.pzh = "0000";
                        md.flxh = 1;
                        md.pzrq = entry.getValue().get(i).pzrq;
                        md.zy = entry.getValue().get(i).zy;
                        result.add(md);
                    } else {
                        md = ncye;
                    }
                    BigDecimal ye = BigDecimalUtil.getData(md.getYe()).add(BigDecimalUtil.getData(entry.getValue().get(i).ye));
                    md.ye = ye;
                    preYe = ye;
                    md.yefx = md.ye.compareTo(BigDecimal.ZERO) == 0 ? 0 : yefx;
                    ncye = md;
                    exists = true;
                } else if (entry.getValue().get(i).tag.equals(2)) {
                    md = new SlmxzDto();
                    entry.getValue().get(i).setPzh("0001");
                    entry.getValue().get(i).setFlxh(1);
                    entry.getValue().get(i).setPzrq(DateUtil.getFirstDayOfMonth(kjnd, entry.getValue().get(i).getKjqj()));
                    BeanUtils.copyProperties(entry.getValue().get(i), md);
                    md.ye = yefx.equals(1) ? preYe.add(md.j.subtract(md.d)) : preYe.add(md.d.subtract(md.j));
                    preYe = md.ye;
                    md.yefx = yefx;
                    result.add(md);
                }
            }
            //添加月度和年度汇总记录
            for (int m = kjqjStart; m <= kjqjEnd; m++) {
                summaryMonth(kjnd, m, false, result);
            }
            Collections.sort(result);
            //计算余额
            calcYe(result, yefx);
            result = result.stream().filter(item -> item.getPzh() == null || item.getPzh().equals("") || item.getPzh().equals("0000")).collect(Collectors.toList());
            entry.setValue(result);
        }

        ZbBookDto dto = new ZbBookDto();
        List<ZbIndexDto> indexList = new ArrayList<>(listMap.size());
        List<ZbTitleDto> contentList = new ArrayList<>();
        ZbIndexDto index;
        ZbTitleDto content;

        String isNj = "";

        //int currentPage = 2 + (kjkmArr.length <= 41 ? 0 : 1);
        int currentPage = 1;
        for (Map.Entry<String, List<SlmxzDto>> entry : listMap.entrySet()) {
            String[] kmArr = entry.getKey().split(" ");

            Optional<GdszczMaEleAcco> kjkm = Globals.kjkmList.stream().filter(c -> c.getAgencyCode().equals(companyId) && c.getSetYear().equals(kjnd) && c.getChrCode().equals(kmArr[0])).findFirst();

            index = new ZbIndexDto();
            index.setCode(entry.getKey());
            if (kjkm.isPresent()) {
                index.setName(kjkm.get().getChrCode() + " " + kjkm.get().getChrName());
            }
            int page = entry.getValue().size() <= 33 ? 1 : (entry.getValue().size() / 33 + (entry.getValue().size() % 33 == 0 ? 0 : 1));
            if (page == 1) {
                index.setPage(String.format("%s", currentPage));
            } else if (page > 1) {
                index.setPage(String.format("%s-%s", currentPage, currentPage + page - 1));
            }
            currentPage += page;
            indexList.add(index);

            content = new ZbTitleDto();
            content.setZtName(dictService.findNameByValue("ZT", ztId));
            if (kjkm.isPresent()) {
                content.setKjkmName(kjkm.get().getChrCode() + " " + kjkm.get().getChrName());
            } else {
                content.setKjkmName(entry.getKey());
            }
            content.setKjnd(kjnd + "年" + isNj);
            content.setPzflList(entry.getValue());
            contentList.add(content);
        }
        dto.setIndex(indexList);
        dto.setContent(contentList);
        return dto;
    }

    //组装核算分组
    private TreeMap<String, List<SlmxzzbDto>> groupByFzhs(TreeMap<String, List<SlmxzzbDto>> dtoList, boolean ajEnable, boolean pjhEnable, boolean zphEnable, boolean wldxEnable, boolean xmEnable, boolean bmEnable) {
        TreeMap<String, List<SlmxzzbDto>> listMap = new TreeMap<>();
        if (ajEnable) {
            for (Map.Entry entry : dtoList.entrySet()) {
                String key = entry.getKey().toString();
                List<SlmxzzbDto> list = (List) entry.getValue();
                TreeMap<String, List<SlmxzzbDto>> tempMap = list.stream().collect(Collectors.groupingBy(item -> key + '-' + item.getAjName(), TreeMap::new, Collectors.toList()));
                for (Map.Entry entry_item : tempMap.entrySet()) {
                    String key_item = entry_item.getKey().toString();
                    key_item = key_item.replaceAll("-null", "");
                    List<SlmxzzbDto> list_item = (List) entry_item.getValue();
                    listMap.put(key_item, list_item);
                }
            }
            dtoList = (TreeMap<String, List<SlmxzzbDto>>) listMap.clone();
            listMap.clear();
        }
        if (pjhEnable) {
            for (Map.Entry entry : dtoList.entrySet()) {
                String key = entry.getKey().toString();
                List<SlmxzzbDto> list = (List) entry.getValue();
                TreeMap<String, List<SlmxzzbDto>> tempMap = list.stream().collect(Collectors.groupingBy(item -> key + '-' + item.getPjhName(), TreeMap::new, Collectors.toList()));
                for (Map.Entry entry_item : tempMap.entrySet()) {
                    String key_item = entry_item.getKey().toString();
                    key_item = key_item.replaceAll("-null", "");
                    List<SlmxzzbDto> list_item = (List) entry_item.getValue();
                    listMap.put(key_item, list_item);
                }
            }
            dtoList = (TreeMap<String, List<SlmxzzbDto>>) listMap.clone();
            listMap.clear();
        }
        if (zphEnable) {
            for (Map.Entry entry : dtoList.entrySet()) {
                String key = entry.getKey().toString();
                List<SlmxzzbDto> list = (List) entry.getValue();
                TreeMap<String, List<SlmxzzbDto>> tempMap = list.stream().collect(Collectors.groupingBy(item -> key + '-' + item.getZphName(), TreeMap::new, Collectors.toList()));
                for (Map.Entry entry_item : tempMap.entrySet()) {
                    String key_item = entry_item.getKey().toString();
                    key_item = key_item.replaceAll("-null", "");
                    List<SlmxzzbDto> list_item = (List) entry_item.getValue();
                    listMap.put(key_item, list_item);
                }
            }
            dtoList = (TreeMap<String, List<SlmxzzbDto>>) listMap.clone();
            listMap.clear();
        }
        if (wldxEnable) {
            for (Map.Entry entry : dtoList.entrySet()) {
                String key = entry.getKey().toString();
                List<SlmxzzbDto> list = (List) entry.getValue();
                TreeMap<String, List<SlmxzzbDto>> tempMap = list.stream().collect(Collectors.groupingBy(item -> key + '-' + item.getWldxName(), TreeMap::new, Collectors.toList()));
                for (Map.Entry entry_item : tempMap.entrySet()) {
                    String key_item = entry_item.getKey().toString();
                    key_item = key_item.replaceAll("-null", "");
                    List<SlmxzzbDto> list_item = (List) entry_item.getValue();
                    listMap.put(key_item, list_item);
                }
            }
            dtoList = (TreeMap<String, List<SlmxzzbDto>>) listMap.clone();
            listMap.clear();
        }
        if (xmEnable) {
            for (Map.Entry entry : dtoList.entrySet()) {
                String key = entry.getKey().toString();
                List<SlmxzzbDto> list = (List) entry.getValue();
                TreeMap<String, List<SlmxzzbDto>> tempMap = list.stream().collect(Collectors.groupingBy(item -> key + '-' + item.getXmName(), TreeMap::new, Collectors.toList()));
                for (Map.Entry entry_item : tempMap.entrySet()) {
                    String key_item = entry_item.getKey().toString();
                    key_item = key_item.replaceAll("-null", "");
                    List<SlmxzzbDto> list_item = (List) entry_item.getValue();
                    listMap.put(key_item, list_item);
                }
            }
            dtoList = (TreeMap<String, List<SlmxzzbDto>>) listMap.clone();
            listMap.clear();
        }
        if (bmEnable) {
            for (Map.Entry entry : dtoList.entrySet()) {
                String key = entry.getKey().toString();
                List<SlmxzzbDto> list = (List) entry.getValue();
                TreeMap<String, List<SlmxzzbDto>> tempMap = list.stream().collect(Collectors.groupingBy(item -> key + '-' + item.getBmName(), TreeMap::new, Collectors.toList()));
                for (Map.Entry entry_item : tempMap.entrySet()) {
                    String key_item = entry_item.getKey().toString();
                    key_item = key_item.replaceAll("-null", "");
                    List<SlmxzzbDto> list_item = (List) entry_item.getValue();
                    listMap.put(key_item, list_item);
                }
            }
            dtoList = (TreeMap<String, List<SlmxzzbDto>>) listMap.clone();
            listMap.clear();
        }
        return dtoList;
    }

    //计算余额
    private <T extends SlmxzDto> void calcYe(List<T> list, Integer yefx) {
        for (int i = 0; i < list.size(); i++) {
            if (i == 0 && list.get(i).getTag().equals(0)) {
                if (list.get(i).ye == null) {
                    list.get(i).ye = BigDecimal.ZERO;
                }
                list.get(i).ye = list.get(i).ye.abs();
            }
            if (i == 1 && list.get(i).getTag().equals(1)) { //期初余额
                if (yefx == 1) {
                    list.get(i).ye = (BigDecimalUtil.getData(list.get(i - 1).ye)).add(BigDecimalUtil.getData(list.get(i).j)).subtract(BigDecimalUtil.getData(list.get(i).d));
                } else {
                    list.get(i).ye = (BigDecimalUtil.getData(list.get(i - 1).ye)).add(BigDecimalUtil.getData(list.get(i).d)).subtract(BigDecimalUtil.getData(list.get(i).j));
                }
                list.get(i).j = BigDecimal.ZERO;
                list.get(i).d = BigDecimal.ZERO;
            } else if (i > 0 && list.get(i).getTag().equals(2)) {
                if (yefx == 1) {
                    list.get(i).ye = (BigDecimalUtil.getData(list.get(i - 1).ye)).add(BigDecimalUtil.getData(list.get(i).j)).subtract(BigDecimalUtil.getData(list.get(i).d));
                } else {
                    list.get(i).ye = (BigDecimalUtil.getData(list.get(i - 1).ye)).add(BigDecimalUtil.getData(list.get(i).d)).subtract(BigDecimalUtil.getData(list.get(i).j));
                }
            } else if (i > 0) {
                list.get(i).ye = BigDecimalUtil.getData(list.get(i - 1).ye);
            }
            list.get(i).yefx = BigDecimalUtil.getData(list.get(i).ye).compareTo(BigDecimal.ZERO) == 0 ? 0 : yefx;
        }
    }

    //计算余额
    private void calcFzxYe(List<SlmxzDto> list, Integer yefx) {
        for (int i = 0; i < list.size(); i++) {
            if (i == 0 && list.get(i).getTag().equals(0) && list.get(i).ye == null) {
                list.get(i).ye = BigDecimal.ZERO;
            } else if (i == 0 && list.get(i).getTag().equals(0) && yefx == 2) {
                list.get(i).ye = list.get(i).ye.negate();
            }
            if (i == 1 && list.get(i).getTag().equals(1)) { //期初余额
                list.get(i).ye = (BigDecimalUtil.getData(list.get(i - 1).ye)).add(BigDecimalUtil.getData(list.get(i).j)).subtract(BigDecimalUtil.getData(list.get(i).d));
                list.get(i).j = BigDecimal.ZERO;
                list.get(i).d = BigDecimal.ZERO;
            } else if (i > 0 && list.get(i).getTag().equals(2)) {
                if (yefx == 2) {
                    list.get(i).ye = (BigDecimalUtil.getData(list.get(i - 1).ye)).add(BigDecimalUtil.getData(list.get(i).d)).subtract(BigDecimalUtil.getData(list.get(i).j));
                } else {
                    list.get(i).ye = (BigDecimalUtil.getData(list.get(i - 1).ye)).add(BigDecimalUtil.getData(list.get(i).j)).subtract(BigDecimalUtil.getData(list.get(i).d));
                }
            } else if (i > 0) {
                list.get(i).ye = BigDecimalUtil.getData(list.get(i - 1).ye);
            }
            list.get(i).yefx = BigDecimalUtil.getData(list.get(i).ye).compareTo(BigDecimal.ZERO) == 0 ? 0 : yefx;
        }

        for (int i = 0; i < list.size(); i++) {
            if (BigDecimalUtil.getData(list.get(i).ye).compareTo(BigDecimal.ZERO) < 0) {
                list.get(i).ye = list.get(i).ye.abs();
                list.get(i).yefx = (3 - list.get(i).yefx);
            }
            if (list.get(i).getTag().equals(1)) {
                if (list.get(i).getJ() == null) {
                    list.get(i).j = BigDecimal.ZERO;
                }
                if (list.get(i).getD() == null) {
                    list.get(i).d = BigDecimal.ZERO;
                }
            }
        }
    }

    // 月度-年度汇总
    private <T extends SlmxzDto> void initNcye(Integer kjnd, List<T> result) {
        if (result.stream().filter(item -> item.getTag().equals(0)).count() == 0) {
            SlmxzDto md_mf = new SlmxzDto();
            md_mf.pzrq = kjnd + "-01-01";
            md_mf.kjqj = 1;
            md_mf.tag = 0;
            md_mf.pzh = "";
            md_mf.flxh = 0;
            md_mf.zy = "年初余额";
            md_mf.j = BigDecimal.ZERO;
            md_mf.d = BigDecimal.ZERO;
            md_mf.ye = BigDecimal.ZERO;
            result.add((T) md_mf);
        }
    }

    // 月度-年度汇总
    private void initNcye1(Integer kjnd, List<SlmxzzbDto> result) {
        if (result.stream().filter(item -> item.getTag().equals(0)).count() == 0) {
            SlmxzzbDto md_mf = new SlmxzzbDto();
            md_mf.pzrq = kjnd + "-01-01";
            md_mf.kjqj = 1;
            md_mf.tag = 0;
            md_mf.pzh = "";
            md_mf.flxh = 0;
            md_mf.zy = "年初余额";
            md_mf.j = BigDecimal.ZERO;
            md_mf.d = BigDecimal.ZERO;
            md_mf.ye = BigDecimal.ZERO;
            result.add((SlmxzzbDto) md_mf);
        }
    }


    // 月度-年度汇总
    private <T extends SlmxzDto> void summaryMonth(Integer kjnd, Integer kjqj, Boolean neYcye, List<T> result) {
        if (neYcye && kjqj > 1) {
            String pzrqFirst = DateUtil.getFirstDayOfMonth(kjnd, kjqj);
            SlmxzDto md_mf = new SlmxzDto();
            md_mf.pzrq = pzrqFirst;
            md_mf.kjqj = kjqj;
            md_mf.tag = 1;
            md_mf.pzh = "";
            md_mf.flxh = 0;
            md_mf.zy = "月初余额";
            md_mf.j = BigDecimal.ZERO;
            md_mf.d = BigDecimal.ZERO;
            result.add((T) md_mf);
        }

        String pzrqLast = DateUtil.getLastDayOfMonth(kjnd, kjqj);
        SlmxzDto md_ms = new SlmxzDto();
        md_ms.pzrq = pzrqLast;
        md_ms.kjqj = kjqj;
        md_ms.flxh = 0;
        md_ms.tag = 3;
        md_ms.pzh = "";
        md_ms.zy = "本月合计";
        grandMonth(result.stream().filter(item -> item.getKjqj().equals(kjqj)).collect(Collectors.toList()), md_ms);
        result.add((T) md_ms);

        SlmxzDto md_ms1 = new SlmxzDto();
        md_ms1.pzrq = pzrqLast;
        md_ms1.kjqj = kjqj;
        md_ms1.flxh = 0;
        md_ms1.tag = 4;
        md_ms1.pzh = "";
        md_ms1.zy = "本年累计";
        grandYear(result.stream().filter(item -> item.getKjqj().compareTo(kjqj) <= 0).collect(Collectors.toList()), md_ms1);
        result.add((T) md_ms1);
    }

    // 月度-年度汇总
    private void summaryMonth1(Integer kjnd, Integer kjqj, Boolean neYcye, List<SlmxzzbDto> result) {
        if (neYcye && kjqj > 1) {
            String pzrqFirst = DateUtil.getFirstDayOfMonth(kjnd, kjqj);
            SlmxzzbDto md_mf = new SlmxzzbDto();
            md_mf.pzrq = pzrqFirst;
            md_mf.kjqj = kjqj;
            md_mf.tag = 1;
            md_mf.pzh = "";
            md_mf.flxh = 0;
            md_mf.zy = "月初余额";
            md_mf.j = BigDecimal.ZERO;
            md_mf.d = BigDecimal.ZERO;
            result.add((SlmxzzbDto) md_mf);
        }

        String pzrqLast = DateUtil.getLastDayOfMonth(kjnd, kjqj);
        SlmxzzbDto md_ms = new SlmxzzbDto();
        md_ms.pzrq = pzrqLast;
        md_ms.kjqj = kjqj;
        md_ms.flxh = 0;
        md_ms.tag = 3;
        md_ms.pzh = "";
        md_ms.zy = "本月合计";
        grandMonth1(result.stream().filter(item -> item.getKjqj().equals(kjqj)).collect(Collectors.toList()), md_ms);
        result.add((SlmxzzbDto) md_ms);

        SlmxzzbDto md_ms1 = new SlmxzzbDto();
        md_ms1.pzrq = pzrqLast;
        md_ms1.kjqj = kjqj;
        md_ms1.flxh = 0;
        md_ms1.tag = 4;
        md_ms1.pzh = "";
        md_ms1.zy = "本年累计";
        grandYear1(result.stream().filter(item -> item.getKjqj().compareTo(kjqj) <= 0).collect(Collectors.toList()), md_ms1);
        result.add((SlmxzzbDto) md_ms1);
    }

    @Override
    public List<XszDto> xsz(Map<String, Object> params) {
        String companyId = params.get("companyId").toString();
        Integer kjnd = Integer.parseInt(params.get("kjnd").toString());
        boolean expecoEnable = params.get("expecoEnable").toString().equals("1");
        boolean expfuncEnable = params.get("expfuncEnable").toString().equals("1");
        boolean projectEnable = params.get("projectEnable").toString().equals("1");
        boolean depproEnable = params.get("depproEnable").toString().equals("1");
        boolean departmentEnable = params.get("departmentEnable").toString().equals("1");
        boolean isShowAll = false;
        if (params.containsKey("isShowAll")) {
            isShowAll = params.get("isShowAll").toString().equals("1");
        }

        params.remove("kjnd");

        List<XszDto> list;
        if (expecoEnable || expfuncEnable || projectEnable || depproEnable || departmentEnable) {
            if (isShowAll) {
                list = reportDao.xsz02(params);
            } else {
                list = reportDao.xsz2(params);
            }
        } else {
            params.remove("expecoCodes");
            params.remove("expfuncCodes");
            params.remove("projectCodes");
            params.remove("depproCodes");
            params.remove("departmentCodes");

            list = reportDao.xsz1(params);
        }

        boolean exists = false;
        String initPzid = null;
        for (int i = 0; i < list.size(); i++) {
            XszDto item = list.get(i);
            if (item.getPzh().equals("0000")) {
                if (!exists && i == 0) {
                    exists = true;
                    initPzid = item.getPzid();
                } else if (!item.getPzid().equals(initPzid)) {
                    list.remove(i);
                    i--;
                }
            }
        }

        for (int i = 0; i < list.size(); i++) {
            list.get(i).yefx = 1;
            if (i == 0) {
                list.get(i).ye = list.get(i).j.subtract(list.get(i).d);
            } else {
                list.get(i).ye = (list.get(i - 1).ye).add(list.get(i).j).subtract(list.get(i).d);
            }
            if (list.get(i).ye.compareTo(BigDecimal.ZERO) == 0) {
                list.get(i).yefx = 0;
            }
        }
        Optional<GdszczMaEleAcco> kjkm;
        for (int i = 0; i < list.size(); i++) {
            XszDto item = list.get(i);
            if (item.ye.compareTo(BigDecimal.ZERO) < 0) {
                item.ye = item.ye.abs();
                item.yefx = (3 - item.yefx);
            }
            kjkm = Globals.kjkmList.stream().filter(c -> c.getAgencyCode().equals(companyId) && c.getSetYear().equals(kjnd) && c.getChrCode().equals(item.getKjkm())).findFirst();
            if (kjkm.isPresent()) {
                item.setKjkm(kjkm.get().getChrCode() + " " + kjkm.get().getChrName());
            }
        }
        return list;
    }


    /**
     * 月末合计
     *
     * @param records
     * @param month
     */
    private <T extends SlmxzDto> void grandMonth(List<T> records, SlmxzDto month) {
        month.j = BigDecimal.ZERO;
        month.d = BigDecimal.ZERO;
        for (SlmxzDto row : records) {
            if (DateUtil.parseDate(row.pzrq).getMonth() == DateUtil.parseDate(month.pzrq).getMonth() && StringUtil.isNotEmpty(row.pzh)) {
                month.j = month.j.add(row.j);
                month.d = month.d.add(row.d);
            }
        }
    }

    /**
     * 本年累计
     *
     * @param records
     * @param month
     */
    private <T extends SlmxzDto> void grandYear(List<T> records, SlmxzDto month) {
        month.j = BigDecimal.ZERO;
        month.d = BigDecimal.ZERO;
        month.flxh = 0;
        for (SlmxzDto row : records) {
            if (DateUtil.parseDate(row.pzrq).getMonth() <= DateUtil.parseDate(month.pzrq).getMonth() && StringUtil.isNotEmpty(row.pzh)) {
                month.j = month.j.add(row.j);
                month.d = month.d.add(row.d);
            }
        }
    }

    /**
     * 月末合计
     *
     * @param records
     * @param month
     */
    private void grandMonth1(List<SlmxzzbDto> records, SlmxzzbDto month) {
        month.j = BigDecimal.ZERO;
        month.d = BigDecimal.ZERO;
        for (SlmxzzbDto row : records) {
            if (DateUtil.parseDate(row.pzrq).getMonth() == DateUtil.parseDate(month.pzrq).getMonth() && StringUtil.isNotEmpty(row.pzh)) {
                month.j = month.j.add(BigDecimalUtil.getData(row.j));
                month.d = month.d.add(BigDecimalUtil.getData(row.d));
            }
        }
    }

    /**
     * 本年累计
     *
     * @param records
     * @param month
     */
    private void grandYear1(List<SlmxzzbDto> records, SlmxzzbDto month) {
        month.j = BigDecimal.ZERO;
        month.d = BigDecimal.ZERO;
        month.flxh = 0;
        for (SlmxzDto row : records) {
            if (DateUtil.parseDate(row.pzrq).getMonth() <= DateUtil.parseDate(month.pzrq).getMonth() && StringUtil.isNotEmpty(row.pzh)) {
                month.j = month.j.add(BigDecimalUtil.getData(row.j));
                month.d = month.d.add(BigDecimalUtil.getData(row.d));
            }
        }
    }

    /**
     * 递归统计父级数据
     */
    private void calcParent(List<KmyebDto> dtoList, KmyebDto parent, int start) {
        KmyebDto temp;
        for (int i = start + 1; i < dtoList.size(); i++) {
            start++;
            temp = dtoList.get(i);
            if (temp.kjkmCode.startsWith(parent.kjkmCode)) {
                if (temp.isMx == 1) {
                    if (parent.yefx.equals(temp.yefx)) {
                        parent.ncye = BigDecimalUtil.getData(parent.ncye).add(BigDecimalUtil.getData(temp.ncye));
                        parent.qcye = BigDecimalUtil.getData(parent.qcye).add(BigDecimalUtil.getData(temp.qcye));
                    } else {
                        parent.ncye = BigDecimalUtil.getData(parent.ncye).add(BigDecimalUtil.getData(temp.ncye).negate());
                        parent.qcye = BigDecimalUtil.getData(parent.qcye).add(BigDecimalUtil.getData(temp.qcye).negate());
                    }

                    parent.jfs = BigDecimalUtil.getData(parent.jfs).add(BigDecimalUtil.getData(temp.jfs));
                    parent.dfs = BigDecimalUtil.getData(parent.dfs).add(BigDecimalUtil.getData(temp.dfs));
                    parent.jlj = BigDecimalUtil.getData(parent.jlj).add(BigDecimalUtil.getData(temp.jlj));
                    parent.dlj = BigDecimalUtil.getData(parent.dlj).add(BigDecimalUtil.getData(temp.dlj));
                    parent.ye = BigDecimalUtil.getData(parent.ye).add(BigDecimalUtil.getData(temp.ye));
                } else {
                    calcParent(dtoList, temp, i);
                }
            }
        }
    }

    /**
     * 递归统计父级数据
     */
    private void calcParent2(List<FzxyebDto> dtoList, FzxyebDto parent, int start) {
        FzxyebDto temp;
        for (int i = start + 1; i < dtoList.size(); i++) {
            start++;
            temp = dtoList.get(i);
            if (temp.kjkmCode.startsWith(parent.kjkmCode)) {
                if (temp.isMx == 1) {
                    if (temp.yefx.equals(parent.yefx)) {
                        parent.ncye = BigDecimalUtil.getData(parent.ncye).add(BigDecimalUtil.getData(temp.ncye));
                        parent.qcye = BigDecimalUtil.getData(parent.qcye).add(BigDecimalUtil.getData(temp.qcye));
                    } else {
                        parent.ncye = BigDecimalUtil.getData(parent.ncye).subtract(BigDecimalUtil.getData(temp.ncye));
                        parent.qcye = BigDecimalUtil.getData(parent.qcye).subtract(BigDecimalUtil.getData(temp.qcye));
                    }
                    parent.jfs = BigDecimalUtil.getData(parent.jfs).add(BigDecimalUtil.getData(temp.jfs));
                    parent.dfs = BigDecimalUtil.getData(parent.dfs).add(BigDecimalUtil.getData(temp.dfs));
                    parent.jlj = BigDecimalUtil.getData(parent.jlj).add(BigDecimalUtil.getData(temp.jlj));
                    parent.dlj = BigDecimalUtil.getData(parent.dlj).add(BigDecimalUtil.getData(temp.dlj));
                    if (temp.yefx.equals(parent.yefx)) {
                        parent.ye = BigDecimalUtil.getData(parent.ye).add(BigDecimalUtil.getData(temp.ye));
                    } else {
                        parent.ye = BigDecimalUtil.getData(parent.ye).subtract(BigDecimalUtil.getData(temp.ye));
                    }
                } else {
                    calcParent2(dtoList, temp, i);
                }
            }
        }
    }
}
